package com.iamback.dy.examine.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamback.dy.entity.ContentExResultCount;
import com.iamback.dy.entity.ContentExamineByTypeCount;
import com.iamback.dy.entity.ContentExamineTotalCount;
import com.iamback.dy.entity.User;
import com.iamback.dy.examine.common.Page;
import com.iamback.dy.examine.service.IContentCountService;
import com.iamback.dy.examine.vo.ContentCountRate;
import com.iamback.dy.mapper.ContentCountMapper;
import com.iamback.dy.mapper.UserMapper;

/**
 * 帖子统计
 */
@Service
public class ContentCountServiceImpl implements IContentCountService {
	@Autowired
	private ContentCountMapper contentCountMapper;
	@Autowired
	private UserMapper userMapper;
	@Override
	public List<ContentCountRate> countRate(String userId,Date startDate,Date endDate,Integer pageNum ,Integer numPerPage) {
		Map<String, Object> map =new HashMap<String, Object>();
//		Page<ContentCountRate> page =new Page<ContentCountRate>();
		if(userId!=null &&!userId.equals("")){
			map.put("userId", userId);
		}
		if(startDate !=null){
			map.put("startDate", startDate);
		}
		if(endDate !=null){
			map.put("endDate", endDate);
		}
		map.put("pageNum", pageNum);
		map.put("numPerPage", numPerPage);
		List<ContentCountRate>  contentCountRates =new ArrayList<ContentCountRate>();
	    List<ContentExResultCount> contentCounts =contentCountMapper.findContentExamineCountByUser(map);
		for(ContentExResultCount contentCount:contentCounts){
			ContentCountRate contentCountRate =new ContentCountRate();
			contentCountRate.setPassCount(contentCount.getPassCount());
			contentCountRate.setRefuseCount(contentCount.getRefuseCount()); 
			contentCountRate.setPassRate(myPercent(contentCount.getPassCount(),(contentCount.getPassCount()+contentCount.getRefuseCount())) ); 
			contentCountRate.setRefuseRate(myPercent(contentCount.getRefuseCount() ,(contentCount.getPassCount()+contentCount.getRefuseCount())) );
			if(contentCount.getUserId()==null ||contentCount.getUserId()==0){
				contentCountRate.setUserId(0);
				contentCountRate.setUserName(null);
			}else{
				contentCountRate.setUserId(contentCount.getUserId());
			    User user= userMapper.getUserById(contentCount.getUserId());
			    contentCountRate.setUserName(user.getUserName());
			}
			contentCountRates.add(contentCountRate);
		}
//		page.setContent(contentCountRates);
//		page.setTotalElements(contentCountMapper.findContentExamineCountByCount(map));
		return contentCountRates;
	}
	
	
	public static String myPercent(int y, int z) { 
		String baifenbi = "";// 接受百分比的值 
		double baiy = y * 1.0; 
	    double baiz = z * 1.0; 
	    double fen = baiy / baiz;  
	    DecimalFormat df1 = new DecimalFormat("#0.00%");
	    baifenbi = df1.format(fen);  
	    return baifenbi; 
	} 
	
	
	@Override
	public List<ContentExamineByTypeCount> findContentExamineByType(String userName ,String dateType, Date startDate,Date endDate){
		Map<String, Object> map = new HashMap<String, Object>();
		if(userName!=null&&!"".equals(userName.trim())){
			map.put("userName", "%"+userName+"%");
		} 
		if(dateType!=null &&!"".equals(dateType.trim())){
			map.put("exTime", "dateType");
			if(dateType.equals("1")){//按照日分组 
				map.put("dateType", "%Y-%m-%d");
			}else if(dateType.equals("2")){//按照月分组
				map.put("dateType", "%Y-%m");
			}
		}
		if(startDate!=null){
			map.put("startDate", startDate);
		}
		if(endDate!=null){
			map.put("endDate", endDate);
		}
		return contentCountMapper.findContentExamineByType(map);
	}
	
	@Override
	public List<ContentExamineTotalCount> findContentExamineTotalCount(Date startDate,Date endDate){
		Map<String, Object> map = new HashMap<String, Object>();
		if(startDate !=null)
		map.put("startDate", startDate);
		if(endDate !=null)
		map.put("endDate", endDate);
		
		return contentCountMapper.findContentExamineTotalCount(map);
	}
	
	

}
